package cn.manjuu.touhou.db.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import cn.manjuu.touhou.APP;
import cn.manjuu.touhou.db.ToHoDBOpenHelper;
import cn.manjuu.touhou.domain.Card;
import cn.manjuu.touhou.util.Looger;

public class CardDao {

	ToHoDBOpenHelper helper = null;
	private static final String TABLE = "card_info";
	public static final String _ID = "_id";
	public static final String PLAYER_ID = "player_id";
	public static final String CARD_ID = "card_id";
	public static final String COUNT = "count";
	public static final String CARD_FILE_NAME = "card_file_name";

	public CardDao(Context context) {
		super();
		this.helper = ToHoDBOpenHelper.getInstance(context);
	}

	public long insert(Card card) {
		SQLiteDatabase db = helper.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put(PLAYER_ID, card.getPlayerId());
		values.put(CARD_ID, card.getCardId());
		values.put(COUNT, card.getCount());
		values.put(CARD_FILE_NAME, card.getCardFileName());

		long id = db.insert(TABLE, null, values);
		db.close();
		return id;
	}

	public int delete(String key, Object value) {
		SQLiteDatabase db = helper.getWritableDatabase();
		int delete = db.delete(TABLE, key + "=?", new String[] { "" + value });
		return delete;
	}
	
	public int deleteAll() {
		SQLiteDatabase db = helper.getWritableDatabase();
		int delete = db.delete(TABLE, null, null);
		return delete;
	}

	public int update(String key, Object value, Card card) {
		SQLiteDatabase db = helper.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put(PLAYER_ID, card.getPlayerId());
		values.put(CARD_ID, card.getCardId());
		values.put(COUNT, card.getCount());
		values.put(CARD_FILE_NAME, card.getCardFileName());
		int i = db.update(TABLE, values, key + "=?",
				new String[] { "" + value });
		db.close();
		return i;
	}

	public Card query(String key, Object value) {
		SQLiteDatabase db = helper.getReadableDatabase();
		Cursor c = db.query(TABLE, null, key + "=?", new String[] { "" + value },
				null, null, null);
		Card card = null;
		while (c.moveToNext()) {
			card = new Card();
			card.set_id(c.getInt(c.getColumnIndex(_ID)));
			card.setPlayerId(c.getLong(c.getColumnIndex(PLAYER_ID)));
			card.setCardId(c.getLong(c.getColumnIndex(CARD_ID)));
			card.setCount(c.getLong(c.getColumnIndex(COUNT)));
			card.setCardFileName(c.getString(c.getColumnIndex(CARD_FILE_NAME)));
		}
		c.close();
		db.close();
		return card;
	}

	public List<Card> queryAll() {
		// TODO Auto-generated method stub
		SQLiteDatabase db = helper.getReadableDatabase();
		Cursor c = db.query(TABLE, null, null, null, null, null, null);
		String value = null;
		List<Card> cards = new ArrayList<Card>();
		Card card = null;
		while (c.moveToNext()) {
			card = new Card();
			card.set_id(c.getInt(c.getColumnIndex(_ID)));
			card.setPlayerId(c.getLong(c.getColumnIndex(PLAYER_ID)));
			card.setCardId(c.getLong(c.getColumnIndex(CARD_ID)));
			card.setCount(c.getLong(c.getColumnIndex(COUNT)));
			card.setCardFileName(c.getString(c.getColumnIndex(CARD_FILE_NAME)));
			Looger.i(APP.CardDao, card.toString());
			cards.add(card);
		}
		c.close();
		db.close();
		return cards;
	}
	
	public List<Long> queryAllCardId() {
		// TODO Auto-generated method stub
		SQLiteDatabase db = helper.getReadableDatabase();
		Cursor c = db.query(TABLE, null, null, null, null, null, null);
		String value = null;
		List<Long> cardIds = new ArrayList<Long>();
		while (c.moveToNext()) {
			cardIds.add(c.getLong(c.getColumnIndex(CARD_ID)));
		}
		c.close();
		db.close();
		return cardIds;
	}

	
	public List<Card> queryAllOrderCardId() {
		// TODO Auto-generated method stub
		SQLiteDatabase db = helper.getReadableDatabase();
		Cursor c = db.query(TABLE, null, null, null, null, null, "card_id");
		String value = null;
		List<Card> cards = new ArrayList<Card>();
		Card card = null;
		while (c.moveToNext()) {
			card = new Card();
			card.set_id(c.getInt(c.getColumnIndex(_ID)));
			card.setPlayerId(c.getLong(c.getColumnIndex(PLAYER_ID)));
			card.setCardId(c.getLong(c.getColumnIndex(CARD_ID)));
			card.setCount(c.getLong(c.getColumnIndex(COUNT)));
			card.setCardFileName(c.getString(c.getColumnIndex(CARD_FILE_NAME)));
			Looger.i(APP.CardDao, card.toString());
			cards.add(card);
		}
		c.close();
		db.close();
		return cards;
	}
}
